﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports C4F.VistaP2P.WPF
Imports System.Windows.Media.Media3D




Namespace ComplexP2PSample
 _
    '/ <summary>
    '/ Interaction logic for Window1.xaml
    '/ </summary>
    Class Window1
        Inherits Window '
        'ToDo: Error processing original source shown below
        '
        '
        '------------^--- 'class', 'struct', 'interface' or 'delegate' expected
        '
        'ToDo: Error processing original source shown below
        '
        '
        '--------------------^--- Syntax error: ';' expected

        'In order to obtain the desired 3d button effect, the buttons are mapped to a plane Mesh in a viewport element,
        'The function OnButtonViewPortMouseDown does a raycast into the viewport in order to see which panel was hit.
        'the appropriate control is then referenced and a button press event is sent to it.
        Private m3DObject As System.Collections.ArrayList
        Private m2DControl As System.Collections.ArrayList
        Private hit As GeometryModel3D 'used in OnButtonViewPortMouseDown to keep track of which panels were hit.
        Private lastHit As GeometryModel3D
        'used in OnButtonViewPortMouseDown to keep track of which panels were hit.
        Public Sub New()
            InitializeComponent()

            Me.UpdateLayout()
            ComponentCanvas.Visibility = Visibility.Hidden
            '////////////////////////////////////////////////////////////////////
            'Set up the 3d buttons.
            '////////////////////////////////////////////////////////////////////
            m2DControl = New System.Collections.ArrayList()
            m3DObject = New System.Collections.ArrayList()

            Dim r3d = New AxisAngleRotation3D(New Vector3D(0, 1, 0), -15)
            Add(Me.mSendPicture, mSendPicture.Width, mSendPicture.Height, New Point3D(-15, 0, -14), New RotateTransform3D(r3d, New Point3D(12, 0, 0)))

            r3d = New AxisAngleRotation3D(New Vector3D(0, 1, 0), -25)
            Add(mSendAudio, mSendAudio.Width, mSendAudio.Height, New Point3D(-13, 1.5, -14), New RotateTransform3D(r3d, New Point3D(12, 0, 0)))

            r3d = New AxisAngleRotation3D(New Vector3D(0, 1, 0), -40)
            Add(mSendVideo, mSendVideo.Width, mSendVideo.Height, New Point3D(-13, 3, -13), New RotateTransform3D(r3d, New Point3D(12, 0, 0)))

            r3d = New AxisAngleRotation3D(New Vector3D(0, 1, 0), -62)
            Add(mSendFile, mSendFile.Width, mSendFile.Height, New Point3D(-13, 4.5, -10), New RotateTransform3D(r3d, New Point3D(12, 0, 0)))


            'Dim  ep As event  = New EventHandler(Of C4F.VistaP2P.Common.PictureChangedEventArgs, picture1.PictureReceived)
            AddHandler pictureControl1.PictureReceived, AddressOf _PictureReceived
            'pictureControl1.PictureReceived += New EventHandler(AddressOf _PictureReceived) '
            'ToDo: Error processing original source shown below
            '
            '
            '----------------------------------------------------------------^--- Syntax error: '(' expected
            '
            'ToDo: Error processing original source shown below
            '
            '
            '-------------------------------------------------------------------------------------------------------------------------------^--- Syntax error: ')' expected

            Me.mediaVideoControl.MediaElement = mMediaVideoViewPort.MediaElement
            AddHandler mediaVideoControl.StartPlayChanged, AddressOf _StartVideoPlayChanged

            mediaAudioControl.MediaElement = mMediaAudioViewPort.MediaElement
            AddHandler mediaAudioControl.StartPlayChanged, AddressOf _StartAudioPlayChanged
        End Sub 'New


        Private Sub _StartVideoPlayChanged(ByVal sender As Object, ByVal e As EventArgs)
            mMediaVideoViewPort.Visibility = Visibility.Visible
            mMediaAudioViewPort.Visibility = Visibility.Hidden
            mImage.Visibility = Visibility.Hidden
        End Sub '_StartVideoPlayChanged


        Private Sub _StartAudioPlayChanged(ByVal sender As Object, ByVal e As EventArgs)
            mMediaVideoViewPort.Visibility = Visibility.Hidden
            mMediaAudioViewPort.Visibility = Visibility.Visible
            mImage.Visibility = Visibility.Hidden
        End Sub '_StartAudioPlayChanged



        Private Sub _PictureReceived(ByVal sender As Object, ByVal e As C4F.VistaP2P.Common.PictureChangedEventArgs) Handles pictureControl1.PictureReceived
            mMediaAudioViewPort.Visibility = Visibility.Hidden
            mMediaVideoViewPort.Visibility = Visibility.Hidden
            mImage.Visibility = Visibility.Visible
        End Sub '_PictureReceived


        '/ <summary>
        '/  Given a normal 2d control, the width, size, it maps the controls visual display to the panel.
        '/  The panel is then placed in the viewport. pos is the position in the viewport and trans is the orientation.
        '/ </summary>
        '/ <param name="l">The 2d control</param>
        '/ <param name="width">the width of the control</param>
        '/ <param name="height">the height of the control</param>
        '/ <param name="pos">the position in the viewport</param>
        '/ <param name="trans">the orientation</param>
        Public Sub Add(ByVal l As Visual, ByVal width As Double, ByVal height As Double, ByVal pos As Point3D, ByVal trans As Transform3D)

            'Store the Visual of the control in the ArrayList, we'll need it later to send it messages
            m2DControl.Add(l)


            Dim p As New Plane()
            Dim model As New ModelVisual3D()

            Dim scale As Double = 0.1
            p.UpperLeft = New Point3D(pos.X + scale * (-width / 2), pos.Y + scale * (-height / 2), pos.Z)
            p.LowerRight = New Point3D(pos.X + scale * width / 2, pos.Y + scale * height / 2, pos.Z)

            'Create the model on which to draw the control's Visual
            Dim modelVisual3d As New ModelVisual3D()
            Dim modelGroup As New Model3DGroup()
            Dim model3d As New GeometryModel3D(p.Mesh, New DiffuseMaterial(New SolidColorBrush(Colors.Green))) 'new GeometryModel3D();
            'perform the transform on the model
            model3d.Transform = trans

            'Create the material with the texture of the Control and use it on the model
            Dim visualMaterial As New DiffuseMaterial(New VisualBrush(l))
            model3d.Material = visualMaterial
            modelGroup.Children.Add(model3d)
            modelVisual3d.Content = modelGroup

            'Store the model in the ArayList, we'll need it for hittesting
            m3DObject.Add(model3d)

            'Add the new plane to the ViewPort, so that we can see it
            buttonViewport.Children.Add(modelVisual3d)
        End Sub 'Add




        '/ <summary>
        '/ When someone clicks on the viewport area, figure out if they hit one of the panels. If so then
        '/ look up which 2d Control was mapped onto that panel.  Then raise the event with the 2d control.
        '/ </summary>
        '/ <param name="sender"></param>
        '/ <param name="e"></param>
        Protected Sub OnButtonViewPortMouseDown(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim mouseposition As System.Windows.Point = CType(e, MouseButtonEventArgs).GetPosition(buttonViewport)
            Dim testpoint3D As New Point3D(mouseposition.X, mouseposition.Y, 0)
            Dim testdirection As New Vector3D(mouseposition.X, mouseposition.Y, 10)
            Dim pointparams As New PointHitTestParameters(mouseposition)
            Dim rayparams As New RayHitTestParameters(testpoint3D, testdirection)
            'test for a result in the Viewport3D   
            hit = Nothing
            VisualTreeHelper.HitTest(buttonViewport, Nothing, AddressOf HTResult, pointparams)
            Dim i As Integer
            For i = 0 To m3DObject.Count - 1
                If Object.Equals(m3DObject(i), hit) Then
                    hit = Nothing
                    Dim hitButton As Button = CType(m2DControl(i), Button)
                    hitButton.RaiseEvent(e)
                End If
            Next i
        End Sub 'OnButtonViewPortMouseDown

        '/ <summary>
        '/ This is used by OnButtonViewPortMouseDown to parse the results of the raycast on the panel.
        '/ </summary>
        '/ <param name="rawresult"></param>
        '/ <returns></returns>
        Public Function HTResult(ByVal rawresult As System.Windows.Media.HitTestResult) As HitTestResultBehavior
            Dim rayResult As RayHitTestResult = rawresult '
            'ToDo: Error processing original source shown below
            '
            '
            '----------------------------------------------------^--- Syntax error: ';' expected
            'if the mouse was clicked on a panel, figure out which one.
            If Not (rayResult Is Nothing) Then
                Dim darkSide As New DiffuseMaterial(New SolidColorBrush(System.Windows.Media.Colors.Red))
                Dim gasit As Boolean = False
                Dim i As Integer
                For i = 0 To m3DObject.Count - 1
                    If Object.Equals(CType(m3DObject(i), GeometryModel3D), rayResult.ModelHit) Then
                        hit = CType(rayResult.ModelHit, GeometryModel3D)
                        gasit = True
                    End If
                Next i
                If Not gasit Then
                    hit = Nothing
                End If
            End If
            Return HitTestResultBehavior.Stop
        End Function 'HTResult



        Private leaving As Boolean
        Private hitButton As Button

        Protected Sub OnButtonViewPortMouseLeave(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        End Sub 'OnButtonViewPortMouseLeave





        Protected Sub hitButton_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        End Sub 'hitButton_MouseEnter

        'bool jkl = this.buttonJoinLeaveNetwork.IsMouseOver;

        Protected Sub OnButtonViewPortMouseEnter(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim mouseposition As System.Windows.Point = CType(e, MouseEventArgs).GetPosition(buttonViewport)
            Dim testpoint3D As New Point3D(mouseposition.X, mouseposition.Y, 0)
            Dim testdirection As New Vector3D(mouseposition.X, mouseposition.Y, 10)
            Dim pointparams As New PointHitTestParameters(mouseposition)
            Dim rayparams As New RayHitTestParameters(testpoint3D, testdirection)
            'test for a result in the Viewport3D   
            hit = Nothing

            VisualTreeHelper.HitTest(buttonViewport, Nothing, AddressOf HTResult, pointparams)
            'if we hit a real panel for the first time.
            If Not Object.Equals(hit, lastHit) And Not (hit Is Nothing) Then
                If leaving Then
                    leaving = False
                    Return
                End If
                Dim i As Integer
                For i = 0 To m3DObject.Count - 1
                    If Object.Equals(m3DObject(i), hit) Then
                        hit = Nothing
                        hitButton = CType(m2DControl(i), Button)

                        ' hitButton.Focus();
                        hitButton.RaiseEvent(e)
                    End If
                Next i

                lastHit = hit
            End If
        End Sub 'OnButtonViewPortMouseEnter


        '/ <summary>
        '/ We need to notify the button on mouse moves as well, to make the UI act appropriately.
        '/ this is just another mapping from the viewport space to the 2d controls.
        '/ </summary>
        '/ <param name="sender"></param>
        '/ <param name="e"></param>
        Protected Sub OnButtonViewPortMouseMove(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        End Sub 'OnButtonViewPortMouseMove
    End Class 'Window1 

    'if (lastHit != null && hit == null)
    '{
    '    //well they must have moved off the panel..free the mouse from the control.
    '    for (int i = 0; i < m3DObject.Count; i++)
    '    {
    '        if (m3DObject[i] == lastHit)
    '        {
    '            Button hitButton = (Button)m2DControl[i];
    '            hitButton.ReleaseMouseCapture();
    '            lastHit = null;
    '        }
    '    }
    '}



    '
    'ToDo: Error processing original source shown below
    '
    '
    '-^--- expression expected
End Namespace 'ComplexP2PSample
